package com.choupin.choupin.controller;

import com.choupin.choupin.admin.service.exception.AuthenticationNotUseException;
import com.choupin.choupin.admin.service.exception.ConferredUponByTheEmperorException;
import com.choupin.choupin.admin.service.exception.DeleteException;
import com.choupin.choupin.entity.JsonResult;
import com.choupin.choupin.service.exception.*;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

public class BaseController {
    protected static final Integer SUCCESS=20;//成功
    protected static final Integer ERROR_LOGIN=25;//登录拦截错误
    protected static final Integer ERROR_SELECT=30;//查询错误
    protected static final Integer ERROR_PASSWORD=31;//密码错误
    protected static final Integer ERROR_CAPTCHA=32;//验证码错误
    protected static final Integer ERROR_FILE_EMPTY=33;//文件为空错误
    protected static final Integer ERROR_FILE_SIZE=34;//大小错误
    protected static final Integer ERROR_FILE_TYPE=35;//文件类型错误
    protected static final Integer ERROR_UPDATE=36;//更新错误
    protected static final Integer ERROR_OLD_PASSWORD=37;//原密码错误
    protected static final Integer ERROR_INCONSISTENCY_PASSWORD=38;//密码不一致
    protected static final Integer ERROR_REDIS_INSERT=39;//Redis插入错误
    protected static final Integer ERROR_AUTHENTICATION_ERROR=40;//验证码失效
    protected static final Integer ERROR_CONFERRED=41;//验证码错误
    protected static final Integer ERROR_DELETE=42;//删除错误
    protected static final Integer ERROR_PARAMETER_IS_EMPTY=300;//参数为空错误


     @ExceptionHandler(ServiceException.class)
     @ResponseBody
     public JsonResult<Void> handlerException(Throwable e){
      JsonResult<Void> jsonResult=new JsonResult<>();
      if ( e instanceof SelectExcepion){
          jsonResult.setState(ERROR_SELECT);
      }else  if (e instanceof PasswordException){
          jsonResult.setState(ERROR_PASSWORD);
      }else if (e instanceof CaptchaException){
          jsonResult.setState(ERROR_CAPTCHA);
      }else if (e instanceof LoginErrorxception){
          jsonResult.setState(ERROR_LOGIN);
      }else if (e instanceof ParameterIsEmptyException){
          jsonResult.setState(ERROR_PARAMETER_IS_EMPTY);
      }else if (e instanceof FileEmptyException){
          jsonResult.setState(ERROR_FILE_EMPTY);
      }else if (e instanceof FileSizeException){
          jsonResult.setState(ERROR_FILE_SIZE);
      }else if (e instanceof FileTypeException){
          jsonResult.setState(ERROR_FILE_TYPE);
      }else if (e instanceof UpdateException){
          jsonResult.setState(ERROR_UPDATE);
      }else if (e instanceof OldPasswordErrorException){
          jsonResult.setState(ERROR_OLD_PASSWORD);
      }else if (e instanceof PasswordInconsistencyException){
          jsonResult.setState(ERROR_INCONSISTENCY_PASSWORD);
      }else if (e instanceof RedisInsertException){
          jsonResult.setState(ERROR_REDIS_INSERT);
      }else if (e instanceof AuthenticationNotUseException){
          jsonResult.setState(ERROR_AUTHENTICATION_ERROR);
      }else if (e instanceof ConferredUponByTheEmperorException){
          jsonResult.setState(ERROR_CONFERRED);
      }else if (e instanceof DeleteException){
          jsonResult.setState(ERROR_DELETE);
      }
       jsonResult.setMessage(e.getMessage());
       return  jsonResult;
     }


}
